package com.poetry.vup.works.controller;

import com.poetry.vup.common.result.BaseResult;
import com.poetry.vup.common.result.page.BasePage;
import com.poetry.vup.works.domain.VupWorksDTO;
import com.poetry.vup.works.domain.VupWorksVO;
import com.poetry.vup.works.service.WorksExhibitionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/works")
public class WorksExhibitionController {

    @Autowired
    private WorksExhibitionService worksExhibitionService;

    /**
     * 查看所有作品
     */

    @RequestMapping(value = "/findWorks",method = RequestMethod.POST)
    public BaseResult findWorks(@RequestParam("pageNum") int pageNum , @RequestParam("pageSize") int pageSize){
        BaseResult baseResult = BaseResult.failed("查找失败");
        BasePage<VupWorksVO> basePage = new BasePage<>();
        BasePage<VupWorksVO> page = worksExhibitionService.findAllWorks(pageSize,pageNum);
        basePage.setTotalPage(page.getTotalPage());
        basePage.setTotal(page.getTotal());
        basePage.setPageSize(page.getPageSize());
        basePage.setPageNum(page.getPageNum());
        basePage.setList(page.getList());
        baseResult = BaseResult.success(basePage);

        return baseResult;
    }

    /**
     * 添加作品
     */
    @PostMapping("addWorks")
    public BaseResult addWorks(@RequestBody VupWorksDTO vupWorksDTO){
        BaseResult<Object> baseResult = BaseResult.failed("");
        Long id = worksExhibitionService.addWorks(vupWorksDTO);
        if(id > 0 ){
            baseResult = BaseResult.success(id);
        }
        return baseResult;
    }


    /**
     * 上传图片
     */
    @PostMapping("/uploadFile")
    public BaseResult uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id){
        BaseResult<Object> baseResult = BaseResult.failed("上传失败");
        if(id == null){
            baseResult = BaseResult.failed("输入参数不全，请检查");
        }else{
            baseResult = worksExhibitionService.uploadFile(file,id);
        }
        return baseResult;
    }



}
